package com.qf.servlet;

import com.qf.entity.Emp;
import com.qf.service.EmpService;
import com.qf.service.impl.EmpServiceImpl;
import com.qf.utils.JsonUtils;
import com.qf.utils.PageUtils;
import com.qf.utils.ResultVo;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

@WebServlet(name = "empServlet",urlPatterns = "/empServlet")
public class EmpServlet extends BaseServlet {
    private EmpService empService=new EmpServiceImpl();

    public void selectEmp(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter pw = response.getWriter();
        ResultVo<PageUtils<Emp>> resultVo=new ResultVo<>();
        String e_name = request.getParameter("e_name");
        String e_phone_number = request.getParameter("e_phone_number");
        String pageSizeStr = request.getParameter("pageSize");
        String currentPageNoStr = request.getParameter("currentPageNo");

        PageUtils<Emp> pageUtils = empService.selectEmp(e_name, e_phone_number, pageSizeStr, currentPageNoStr);
        if (pageUtils!=null){
            resultVo.setData(pageUtils);
            resultVo.setMessage("成功");
            resultVo.setCode(200);
        }else {
            resultVo.setData(null);
            resultVo.setMessage("失败");
            resultVo.setCode(500);
        }
        JsonUtils.showJson(pw,resultVo);

    }

    public void selectEmpById(HttpServletRequest request,HttpServletResponse response) throws IOException {
        PrintWriter pw = response.getWriter();
        ResultVo<Emp> resultVo=new ResultVo<>();
        String e_id = request.getParameter("e_id");

        Emp emp = empService.selectEmpById(Integer.parseInt(e_id));
        if (emp!=null){
            resultVo.setData(emp);
            resultVo.setMessage("成功");
            resultVo.setCode(200);
        }else {
            resultVo.setData(null);
            resultVo.setMessage("失败");
            resultVo.setCode(500);
        }
        JsonUtils.showJson(pw,resultVo);
    }

    public void updateEmp(HttpServletRequest request,HttpServletResponse response) throws Exception {
        PrintWriter pw = response.getWriter();
        ResultVo<Boolean> resultVo=new ResultVo<>();
        String e_id = request.getParameter("e_id");
        String realPath = request.getServletContext().getRealPath("WEB-INF/upload");
        File f=new File(realPath);
        if (!f.exists()){
            f.mkdirs();
        }

        boolean b = ServletFileUpload.isMultipartContent(request);
        Emp emp=new Emp();
        boolean flag=false;
        if (b){
            FileItemFactory fileItemFactory=new DiskFileItemFactory();
            //获取文件上传的核心对象
            ServletFileUpload upload=new ServletFileUpload(fileItemFactory);

            List<FileItem> list = upload.parseRequest(request);

            for (FileItem fileItem:list){
                if (fileItem.isFormField()){
                    String name = fileItem.getFieldName();
                    if ("e_password".equals(name)){
                        emp.setE_password(fileItem.getString("UTF-8"));
                    }
                }else {
                    String name = fileItem.getName();
                    if (name.endsWith("jpg") || name.endsWith("png") || name.endsWith("jpeg")){
                        String photoName = System.currentTimeMillis() + name;
                        File file=new File(f,photoName);
                        emp.setE_photo(photoName);
                        fileItem.write(file);
                        flag=true;
                    }else {
                        resultVo.setCode(500);
                        resultVo.setMessage("图片格式不正确");
                        resultVo.setData(false);
                    }
                }
            }
        }else {
            resultVo.setData(false);
            resultVo.setMessage("不是以文件的形式上传");
            resultVo.setCode(500);
        }

        Date date=new Date();
        emp.setE_hiredate(date);

        emp.setE_id(Integer.parseInt(e_id));
        String password = request.getParameter("e_password");
        emp.setE_password(password);
        int num = empService.updateEmp(emp);
        if (flag) {
            if (num>0){
                resultVo.setCode(200);
                resultVo.setMessage("修改成功");
                resultVo.setData(true);
            }else {
                resultVo.setData(false);
                resultVo.setMessage("修改失败");
                resultVo.setCode(500);
            }
        }else {
            resultVo.setCode(500);
            resultVo.setMessage("上传图片不成功");
            resultVo.setData(false);
        }
        JsonUtils.showJson(pw,resultVo);

    }

    public void addEmp(HttpServletRequest request,HttpServletResponse response) throws Exception {
        PrintWriter pw = response.getWriter();
        ResultVo<Boolean> resultVo=new ResultVo<>();

        String realPath = request.getServletContext().getRealPath("WEB-INF/upload");
        File f=new File(realPath);
        if (!f.exists()){
            f.mkdirs();
        }

        boolean b = ServletFileUpload.isMultipartContent(request);
        Emp emp=new Emp();
        boolean flag=false;
        if (b){
            FileItemFactory fileItemFactory=new DiskFileItemFactory();
            //获取文件上传的核心对象
            ServletFileUpload upload=new ServletFileUpload(fileItemFactory);

            List<FileItem> list = upload.parseRequest(request);

            for (FileItem fileItem:list){
                if (fileItem.isFormField()){
                    String name = fileItem.getFieldName();
                    if ("e_password".equals(name)){
                        emp.setE_password(fileItem.getString("UTF-8"));
                    }else if ("e_name".equals(name)){
                        emp.setE_name(fileItem.getString("UTF-8"));
                    }else if ("e_phone_number".equals(name)){
                        emp.setE_phone_number(fileItem.getString("UTF-8"));
                    }

                }else {
                    String name = fileItem.getName();
                    if (name.endsWith("jpg") || name.endsWith("png") || name.endsWith("jpeg")){
                        String photoName = System.currentTimeMillis() + name;
                        File file=new File(f,photoName);
                        emp.setE_photo(photoName);
                        fileItem.write(file);
                        flag=true;
                    }else {
                        resultVo.setCode(500);
                        resultVo.setMessage("图片格式不正确");
                        resultVo.setData(false);
                    }
                }
            }
        }else {
            resultVo.setData(false);
            resultVo.setMessage("不是以文件的形式上传");
            resultVo.setCode(500);
        }

        Date date=new Date();
        emp.setE_hiredate(date);

        int num = empService.addEmp(emp);
        if (flag) {
            if (num>0){
                resultVo.setCode(200);
                resultVo.setMessage("添加成功");
                resultVo.setData(true);
            }else {
                resultVo.setData(false);
                resultVo.setMessage("添加失败");
                resultVo.setCode(500);
            }
        }else {
            resultVo.setCode(500);
            resultVo.setMessage("上传图片不成功");
            resultVo.setData(false);
        }
        JsonUtils.showJson(pw,resultVo);
    }

    public void deleteEmp(HttpServletRequest request,HttpServletResponse response) throws IOException {
        PrintWriter pw = response.getWriter();
        ResultVo<Boolean> resultVo=new ResultVo<>();
        String e_id = request.getParameter("e_id");
        int num = empService.deleteEmp(Integer.parseInt(e_id));
        if (num>0){
            resultVo.setCode(200);
            resultVo.setMessage("删除成功");
            resultVo.setData(true);
        }else {
            resultVo.setData(false);
            resultVo.setMessage("删除失败");
            resultVo.setCode(500);
        }
        JsonUtils.showJson(pw,resultVo);
    }
}
